//开发平台的基础功能接口
import { useAxios } from '@/hooks/web/useAxios'
import { useCache } from '@/hooks/web/useCache'
const request = useAxios()
const { wsCache } = useCache()
const Cache_Modules = 'ezlcp-modules'
/**
 * 实体选择数据源(传入模块ID)
 */
export const selectDataSource = (moduleId) => {
  return request.postXww({
    url: 'form/datasource/selectDataSource',
    params: { moduleId: moduleId }
  })
}

/**
 * 查询数据源
 */
export const queryDataSource = (data) => {
  return request.post({
    url: 'form/datasource/query',
    data
  })
}

/**
 * @description  删除数据源
 */
export const delDataSource = (id) => {
  return request.postXww({
    url: 'form/datasource/del',
    params: { id: id }
  })
}

/**
 * @description  检查数据源标识符是否重复
 */
export const checkDsRepeat = (identifier, dsId) => {
  return request.postXww({
    url: 'form/datasource/checkDsRepeat',
    params: { identifier: identifier, dsId: dsId }
  })
}

/**
 * @description  删除数据源
 */
export const getDataSource = (id) => {
  return request.postXww({
    url: 'form/datasource/get',
    params: { pkId: id }
  })
}

/**
 * @description 保存数据源
 */
export const saveDataSource = (data) => {
  return request.post({
    url: 'form/datasource/save',
    data: data
  })
}

/**
 * @description 测试数据源连接
 */
export const testConnection = (dsId) => {
  return request.postXww({
    url: 'form/datasource/testConnection',
    params: { dsId: dsId }
  })
}

/**
 * 查询数据字典
 */
export const queryDictionary = (data) => {
  return request.post({
    url: 'form/dictionary/query',
    data
  })
}

/**
 * @description  删除数据字典
 */
export const delDictionary = (id) => {
  return request.postXww({
    url: 'form/dictionary/del',
    params: { id: id }
  })
}

/**
 * @description  按模块ID，查询数据字典
 */
export const getDicByModuleId = (moduleId) => {
  return request.postXww({
    url: 'form/dictionary/getByModuleId',
    params: { moduleId: moduleId }
  })
}
/**
 * @description  检查数据字典Key是否重复
 */
export const checkDictKey = (dicKey, dicId) => {
  return request.postXww({
    url: 'form/dictionary/checkKey',
    params: { dicKey: dicKey, dicId: dicId }
  })
}

/**
 * @description  查询数据字典
 */
export const getDictionary = (id) => {
  return request.postXww({
    url: 'form/dictionary/get',
    params: { pkId: id }
  })
}

/**
 * @description 保存数据字典
 */
export const saveDictionary = (data) => {
  return request.post({
    url: 'form/dictionary/saveDict',
    data: data
  })
}

/**
 * 查询单据编号
 */
export const queryOrderNo = (data) => {
  return request.post({
    url: 'form/serialNo/query',
    data
  })
}

/**
 * @description  删除单据编号
 */
export const delOrderNo = (id) => {
  return request.postXww({
    url: 'form/serialNo/del',
    params: { id: id }
  })
}

/**
 * @description 保存单据编号
 */
export const saveOrderNo = (data) => {
  return request.post({
    url: 'form/serialNo/save',
    data: data
  })
}

/**
 * 选择模块的下拉框数据来源
 */
export const selectModule = async () => {
  let data = wsCache.get(Cache_Modules)
  if (!data) {
    data = (await selectModuleApi()).data as any
    wsCache.set(Cache_Modules, data, { exp: 60 * 60 * 8 })
  }
  return data
}

const selectModuleApi = () => {
  return request.get({
    url: 'form/module/selectModule'
  })
}

/**
 * 模块查询
 * @param data 搜索条件
 */
export const queryModules = (data) => {
  return request.post({
    url: 'form/module/query',
    data
  })
}

/**
 * @description  删除模块
 */
export const delModule = (id) => {
  wsCache.delete(Cache_Modules)
  return request.postXww({
    url: 'form/module/del',
    params: { id: id }
  })
}

/**
 * @description 保存模块
 */
export const saveModule = (data) => {
  wsCache.delete(Cache_Modules)
  return request.post({
    url: 'form/module/save',
    data: data
  })
}

/**
 * @description 查詢某个模块的可选择的父节点
 * @author Elwin ZHANG
 */
export const selectParentModules = (moduleId: string) => {
  return request.postXww({
    url: 'form/module/selectParent',
    params: { moduleId: moduleId }
  })
}
